package com.example.service.impl;

import com.example.entity.Account2;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;


    @Override
    public boolean register(Account2 account) {
        if (accountMapper.selectByAccount(account.getAccountnumber()) != null) {
            return false; // 账户已存在
        }
        accountMapper.insertAccount(account);
        return true;
    }

    @Override
    public Account2 login(String accountnumber, String password) {
    Account2 existingAccount = accountMapper.selectByAccount(accountnumber);
    if (existingAccount != null && existingAccount.getPassword().equals(password)) {
        return existingAccount;
    }
    return null;
   }

    @Override
    public boolean updateAccount(Account2 account) {
        try {
            accountMapper.updateAccount(account);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateRole(String accountnumber) {
        try {
            accountMapper.updateRole(accountnumber);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
